package core.utils;

import org.springframework.beans.BeanUtils;

public class MyBeanUtils <Dto, Do>{
    /**
     * dto 转换成 do 的工具类
     */
    public static <Do> Do dtoToDo(Object dtoEntity, Class<Do> doClass){
        if (dtoEntity == null){
            return null;
        }
        if (doClass == null){
            return null;
        }
        try {
            Do newInstance = doClass.newInstance();
//          dto 转 do
            BeanUtils.copyProperties(dtoEntity, newInstance);
            return newInstance;
        }catch (Exception e){
            return null;
        }
    }

    public static <Dto> Dto doToDto(Object doEntity, Class<Dto> dtoClass){
        if (doEntity == null){
            return null;
        }
        if (dtoClass == null){
            return null;
        }
        try {
            Dto newInstance = dtoClass.newInstance();
//          do 转 dto
            BeanUtils.copyProperties(doEntity, newInstance);
            return newInstance;
        }catch (Exception e){
            return null;
        }
    }
}
